<?php

/**
 *   OPENSHOP 订单管理
 *
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: order.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      jiajun <jiajun@baison.com.cn>
 */

define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require(ROOT_PATH . '/includes/modules/taobao.api/cls_api.php');
$exc_type 			= 	new exchange($os->table('storekeeper_type'), $db, 'type_id', 'type_name');
$exc_sk 			= 	new exchange($os->table('storekeeper'), $db, 'sk_id', 'sk_name');
$action_name_list = get_action_name_list();

$action_code = array();
$action_name = array();

if($action_name_list != 'all'){

	foreach ($action_name_list as $item) {
		$action_code[] = $item['action_code'];
		$action_name[] = $item['action_name'];
	}

}else{
	$action_code[] = 'all';
}
/*------------------------------------------------------ */
//-- 扫描出库
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'order_ship')
{
	/* 检查权限 */
	//admin_priv('order_ship');

	/* 模板赋值 */
	$smarty->assign('ur_here', '扫描出库');

	$smarty->assign('action_link', array('href' => 'order.php?act=shipping_list', 'text' => '仓管待处理订单'));

	$smarty->assign('full_page',        1);

	/* 显示模板 */
	assign_query_info();

	$smarty->display('order_ship.tpl');
}
/*------------------------------------------------------ */
//-- 重新发货
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 're_send')
{
	$o_id = $_GET['order_id'];
	$o_sn = $_GET['order_sn'];

	if(!empty($o_id)){
		//更新扫描数量
		$sql = "update open_order_goods set send_number = 0 where order_id = ".$o_id;
		$db->query($sql);
	}
	
	/* 提示信息 */
	$link[] = array('text' => '扫描出库', 'href' => 'order2.php?act=goods_ship&order_sn='.$o_sn );
	//$link[] = array('text' => '扫描出库', 'href' => 'order2.php?act=order_ship');
	sys_msg('该订单已经更新成功，可以进行发货处理！', 0, $link);
}

/*------------------------------------------------------ */
//-- 查询报表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'order_shiping_query')
{

	/* 载入配送方式 */
	$smarty->assign('shipping_list', shipping_list());

	/* 载入支付方式 */
	$smarty->assign('pay_list', payment_list());

	/* 载入国家 */
	$smarty->assign('country_list', get_regions());

	$sql = 'select s.id,s.station_name from '.$os->table('delivery_station').' as s order by s.id asc';
	$station_list = $db->getAll($sql);
	$smarty->assign('station_list',$station_list);

	/* 模板赋值 */
	$smarty->assign('ur_here', '发货统计');
	$smarty->assign('action_link', array('href' => 'order.php?act=shipping_list', 'text' => '待发货列表'));

	/* 显示模板 */
	assign_query_info();

	$smarty->display('order_shiping_query.tpl'); 
}
/*------------------------------------------------------ */
//-- 查询报表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'do_order_shiping_query')
{
	$order_list = get_shipped_order_list();
	
	if($_REQUEST['search_time']=='1'){
		$smarty->assign('time_span', '从 '.$_REQUEST['start_time_id'].' 到 '.$_REQUEST['end_time_id']);
	}elseif($_REQUEST['search_time']=='0'){
		$smarty->assign('time_span', '今天');
	}else{
		$smarty->assign('time_span', '全部历史记录');
	}
 	
	$smarty->assign('order_list',   $order_list['orders']);
	$smarty->assign('record_count', $order_list['record_count']);
	$smarty->assign('print_time', date('Y-m-d H:i:s',time()) );
	
	$smarty->display('order_shipping_print.tpl'); 
}
/*------------------------------------------------------ */
//-- 导出Excel报表
/*------------------------------------------------------ */
elseif($_REQUEST['act'] == 'do_ecport_to_excel'){
	
	$Date = date("Y-m-d");
	$Filename = '快递单导出报表_'.$Date.".csv";
	$Filename = iconv('utf-8','gb2312',$Filename);

	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:filename=".$Filename);
	
	$order_list = get_shipped_order_list();
	
	echo iconv('utf-8','gb2312',"订单编号,快递单号,物流公司,下单时间,收货人,发货时间 \n");
	foreach ($order_list['orders'] as $rs){
	 	echo iconv('utf-8','gb2312',$rs['order_sn']).','. 
	 	iconv('utf-8','gb2312',$rs['invoice_no']).",". 
	 	iconv('utf-8','gb2312',$rs['shipping_name']).",". 
	 	iconv('utf-8','gb2312',$rs['formated_add_time']).",". 
	 	iconv('utf-8','gb2312',$rs['consignee']).",". 
	 	iconv('utf-8','gb2312',$rs['formated_shipping_time'])." \n ";
	}
}
/*------------------------------------------------------ */
//-- 根据商品货号查询相关订单信息
/*------------------------------------------------------ */
elseif($_REQUEST['act'] == 'query_goods_sn'){
	
	$order_list = query_order_by_goods_sn();
	
	if(!empty($order_list)){
		
		echo "<table style='padding:30px;border:solid;'>";
		echo "<tr>";
		echo "<td width=100>订单号</td><tdwidth=100>快递公司</td><tdwidth=100>快递单号</td>";
		foreach ($order_list as $order){
			echo "<tr>";
			echo "<td>".$order['order_sn']."</td><td>".$order['shipping_name']."</td><td>".$order['invoice_no']."</td>";
			echo "</tr>";
			
		}
		echo "</table>";
	}else{
		echo "没有对应的订单记录";	
	}
	
}
/*------------------------------------------------------ */
//-- 订单导出
/*------------------------------------------------------ */
elseif($_REQUEST['act'] == 'order_export_infos'){
	
	$order_list = query_order_shipped();
	$total = 0;
	foreach ($order_list as $order){
		$total += $order['number'];
	}
	if(!empty($order_list)){
		echo "总订单数：".sizeof($order_list)."<span style='padding-left:50px;'>商品总数：".$total."</span>";
		echo "<table style='padding:30px;border:solid;width:100%'>";
		echo "<tr><td width=110>订单号</td>
				<td width=100>发货状态</td>
				<td width=170>物流公司</td>
				<td width=150>发货时间</td>
				<td width=50>数量</td>
				<td >备注</td>
				</tr>";
		foreach ($order_list as $order){
			$shiping2 = $order['shipping_status']=='1'?'已发货':'未发货';
			echo "<tr>";
			echo "<td width=110><a target='_blank' href='order.php?act=info&order_id=".$order['order_id']."'>".$order['order_sn']."</a></td>
			<td width=100>".$shiping2."</td>
			<td>".$order['shipping_name']."</td>
			<td width=150>".$order['shipping_time']."</td>
			<td width=50>".$order['number']."</td>
			<td >".$order['to_buyer']."</td>";
			echo "</tr>";
		}
		echo "</table>";
	}else{
		echo "没有对应的订单记录";	
	}

}
/*------------------------------------------------------ */
//-- 订单导出
/*------------------------------------------------------ */
elseif($_REQUEST['act'] == 'excel_order_infos'){
	
	$Date = date("Y-m-d");
	$Filename = '已发货单导出报表_'.$Date.".csv";
	$Filename = iconv('utf-8','gb2312',$Filename);
	
	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:filename=".$Filename);
	
	$order_list = query_order_shipped();
	
	echo iconv('utf-8','gb2312',"订单编号,发货状态,物流公司,发货时间,发货数量,备注 \n");
	foreach ($order_list as $rs){
		$shiping2 = ($rs['shipping_status']=='1'?'已发货':'未发货');
		
	 	echo iconv('utf-8','gb2312',$rs['order_sn']).','. 
	 	iconv('utf-8','gb2312',$shiping2).",". 
	 	iconv('utf-8','gb2312',$rs['shipping_name']).",". 
	 	iconv('utf-8','gb2312',$rs['shipping_time']).",". 
	 	iconv('utf-8','gb2312',$rs['number']).",".
	 	iconv('utf-8','gb2312',$rs['to_buyer'])." \n ";
	}
	
}
/*------------------------------------------------------ */
//-- 扫描出库处理
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'goods_ship')
{

	/* 检查权限 */
//	admin_priv('goods_ship');

	$order_sn = strtolower(trim($_REQUEST['order_sn']));
	$ship_sn  = trim($_REQUEST['ship_sn']);
	$goods_barcode = strtolower(trim($_REQUEST['goods_barcode']));
	
	//获取订单信息
	$order = order_info(0,$order_sn);
	
	$order_status = $order['order_status'];
	$process_status = $order['process_status'];
	$invoice_status = $order['invoice_status'];
	$shipping_status = $order['shipping_status'];
	
//	if($invoice_status == 0){
//		sys_msg('你还没有打印此订单相应配货单,不能进行扫描出库');
//	}

	if(empty($order)){
		sys_msg('你输入的订单号不存在');
	}
	if($order_status != OS_CONFIRMED){
		sys_msg('该订单还没有经过确认，不能发货');
	}
	
	if($process_status == OPS_SUSPEND){
		sys_msg('该订单已经挂起，不能发货');
	}
	
	if($shipping_status == SS_SHIPPED){
		sys_msg('该订单已经发货');
	}
	
	$smarty->assign('focus',0);

	if($order_sn != '' && $ship_sn == '' && $goods_barcode == ''){
		$smarty->assign('focus',1);
	}

	if($order_sn != '' && $ship_sn != '' && $goods_barcode == ''){
		$smarty->assign('focus',2);
	}
	
	/* 取得订单商品 */
	$goods_list = array();
	$goods_attr = array();

	//未处理的商品记录数
	$i = 0;
	$goods_scan = '';
	$error_flag = 0;
	
	//已经处理的订单商品条数
	$ship_flag = 0;
	
	$g_sn = '';
	$g_color_code = '';
	$g_size_code = '';
	
	//获取扫描的商品信息
	if ($goods_barcode != '') {
		
		/***** 九牧王特有规则 ***********************************************/
		/* 根据第三位判断是22位规则，还是25位规则*/
		$trd_num = substr($goods_barcode,2,1);
		if(preg_match('/^\d*$/',$trd_num)){
			//是数字
			$g_sn = substr($goods_barcode,0,7);
			$g_color_code = substr($goods_barcode,7,2);
			$g_size_code = substr($goods_barcode,9,2);
		}else{
		    $g_sn = substr($goods_barcode,0,10);
			$g_color_code = substr($goods_barcode,10,2);
			$g_size_code = substr($goods_barcode,12,2);    
		}
		/***** 九牧王用代码 ***********************************************/
		
		/***** 非九牧王用代码 ***********************************************/
		// 根据商品条码来获得当前商品的id,尺寸id以及颜色id
//		$sql = "select t.SPDM as goods_sn,t.GG1DM as color_code,t.GG2DM as size_code from ".$os->table('tmdzb').
//		" as t where t.SPTM = '".$goods_barcode."'";
//		$goods_scan = $db->getAll($sql);
//		
//		$g_sn = $goods_scan[0]['goods_sn'];
//		$g_color_code = $goods_scan[0]['color_code'];
//		$g_size_code = $goods_scan[0]['size_code'];
		/***** 非九牧王用代码 ***********************************************/
	}

	//获取订单商品信息
	$sql = "SELECT  o.*,co.color_code,co.color_name,s.size_code,s.size_name,g.goods_number AS storage,
		o.goods_attr " .
		" FROM " . $os->table('order_goods') . " AS o ".
		" LEFT JOIN " . $os->table('goods') . " AS g ON o.goods_id = g.goods_id " .
		" LEFT JOIN " . $os->table('brand') . " AS b ON g.brand_id = b.brand_id " .
		" LEFT JOIN ".$GLOBALS['os']->table('color') ." as co on o.color_id = co.color_id ".
		" LEFT JOIN ".$GLOBALS['os']->table('goods_color') ." as gc on o.color_id = gc.color_id and o.goods_id = gc.goods_id".
		//" left join ".$GLOBALS['os']->table('color_lib') ." as cl on gc.img_id = cl.img_id ".
		" LEFT JOIN ".$GLOBALS['os']->table('size') ." as s on o.size_id = s.size_id ".

		" WHERE o.order_id = '$order[order_id]' ";
		
//		" left join ".$GLOBALS['os']->table('tmdzb') ." as tm on co.color_code = tm.gg1dm and s.size_code = tm.gg2dm ".
//		" and g.goods_sn = tm.spdm ".
//	 die($sql);
	$res = $db->query($sql);
	while ($row = $db->fetchRow($res))
	{
		/* 虚拟商品支持 */
		if ($row['is_real'] == 0)
		{
			/* 取得语言项 */
			$filename = SOURCE_PATH . 'plugins/' . $row['extension_code'] . '/languages/common_' . $_CFG['lang'] . '.php';
			if (file_exists($filename))
			{
				include_once($filename);
				if (!empty($_LANG[$row['extension_code'].'_link']))
				{
					$row['goods_name'] = $row['goods_name'] . sprintf($_LANG[$row['extension_code'].'_link'], $row['goods_id'], $order['order_sn']);
				}
			}
		}
		
		$row['formated_goods_price']      = price_format($row['goods_price']);
	   	//$row['discount_price']    	  	  = round($row['goods_price'] * $discount_rate,2);
	   	//$row['formated_discount_price']   = price_format(round($row['goods_price'] * $discount_rate,2));
	   	$row['subtotal']         		  = $row['goods_price'] * $row['goods_number'];
	   	
	   	//*************************九牧王专用代码,产品化时需要去掉******************************************
	   	
	   	$str_code = strtolower($row['goods_sn'].$row['color_code'].$row['size_code']);
	   	$tem_code = $str_code;
	   	if(strlen($row['goods_sn']) == 7){
	   		for($m =0;$m<(22-strlen($str_code));$m++){
	   			$tem_code.=rand(0,9);
	   		}
	   	}elseif(strlen($row['goods_sn']) == 10){
	   		for($n =0;$n<25-strlen($str_code);$n++){
	   			$tem_code.=rand(0,9);
	   		}
	   	}
	   	$row['sptm'] = $tem_code;

	   	//*************************九牧王专用代码******************************************

	   	$goods_attr[] = explode(' ', trim($row['goods_attr'])); //将商品属性拆分为一个数组

	   	if ($row['extension_code'] == 'package_buy')
	   	{
	   		$row['package_goods_list'] = get_package_goods($row['goods_id']);
	   	}

	   	$img_item = '<img src="'.$_CFG['pre_fetch_img'].$row['img_file'].'?40,40" border="0" width="40" height="40" alt="'.$row['img_name'].'"/>';

	   	$row['img_item'] = $img_item;

		if ($row['extension_code'] == 'package_buy')
		{
			$row['package_goods_list'] = get_package_goods($row['goods_id']);
		}

		// 如果扫描了商品，则进行商品的相关处理，商品条码、扫描数量
		if ($g_sn != '' && $g_color_code != '' && $g_size_code != ''){
			if ($g_sn == strtolower($row['goods_sn']) && $g_color_code == strtolower($row['color_code']) && $g_size_code == strtolower($row['size_code'])) {

				if(($row['send_number'] + 1) > $row['goods_number'] ){
					//你输入的商品已超出了订单中商品的数量!
					$msg_flag = 1;
				}else{
					$row['goods_barcode'] = $goods_barcode; //,goods_barcode = '.$goods_barcode.'
					$row['send_number'] = $row['send_number'] + 1;
					$sql = 'update '.$os->table('order_goods').' set send_number = send_number+1 where rec_id = '.$row['rec_id'];
					$db->query($sql);
				}
			}else{
				$i++;
			}
		}

		$sql = 'select send_number = goods_number from '.$os->table(order_goods).' where rec_id = '.$row['rec_id'];
//		echo $sql;
		$count = $db->getOne($sql);

		if($count){
			$row['scan_finish'] = true;
			$ship_flag++;
		}else { 
				if($order_sn != '' && $ship_sn == '' && $goods_barcode == ''){
					$smarty->assign('focus',1);
				}else {
					$smarty->assign('focus',2);///by winner addd多条商品的时候订位光标
				} 
		}

		$goods_list[] = $row;
	}

	if($i == sizeof($goods_list)){
		//你输入的商品不在此订单中!
		$msg_flag = 2;
		$smarty->assign('focus',2);
	}

	if($ship_flag == sizeof($goods_list)){
		//你扫描的订单已经配货出库完成!
		$msg_flag = 3;
		$smarty->assign('focus',0);
		$shipping_status = SS_SHIPPED;
		$action_note = '扫描出库';
		$order_id = $order['order_id'];

		$order_ship_status = $order['shipping_status'];
		
		if($order_ship_status == SS_UNSHIPPED){
		
			$log_already = order_action_judge($order['order_sn'], OS_CONFIRMED, $shipping_status, $order['pay_status'], $action_note);
			if($log_already){
				os_header("Location: order.php?act=info&order_id=".$order_id. "\n");
				exit;
			}else{
				/* 取得发货单号 */
				$Arr=array('\\'=>'','/'=>'','['=>'',']'=>'','('=>'',')'=>''); 
				$invoice_no = strtr(trim($ship_sn),$Arr);
				
				/* 对虚拟商品的支持 */
				$virtual_goods = get_virtual_goods($order_id);
				if (!empty($virtual_goods))
				{
					if (!virtual_goods_ship($virtual_goods, $msg, $order['order_sn']))
					{
						$links[] = array('text' => '订单信息', 'href' => 'order.php?act=info&order_id=' . $order_id);
						sys_msg($msg, 0, $links);
					}
				}

				if ($order['order_status'] != OS_CONFIRMED)
				{
					$arr['order_status']    = OS_CONFIRMED;
					$arr['confirm_time']    = gmtime();
				}
				$arr['shipping_status']     = $shipping_status;
				$arr['shipping_time']       = gmtime();
				$arr['invoice_no']          = $invoice_no;

				update_order($order_id, $arr);
				/* 记录log */
				order_action($order['order_sn'], $order['order_status'], $shipping_status, $order['pay_status'], $action_note);
//				调用taobao发货 
				taobaoSendOrder($order['order_sn']) ; //调用taobao发货 成功返回0 失败返回0  by winner add 2009-10-24 
				
				$order_sn = '';
				$ship_sn = '';
			}

			/* 记录log */

			/* 如果订单用户不为空，计算积分，并发给用户；发红包 */
			if ($order['user_id'] > 0)
			{
				/* 取得用户信息 */
				$user = user_info($order['user_id']);

				/* 计算并发放积分 */
				$integral = integral_to_give($order);

				log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf('订单 %s 赠送的积分', $order['order_sn']));

				/* 发放红包 */
				send_order_bonus($order_id);
			}
			
			if ($order['order_status'] == OS_UNCONFIRMED)
			{
				change_goods_inventory($order['order_id'],true,1);
			}else{
				change_goods_inventory($order['order_id'],true,1,'ship');
			}
			
			
			/* 发送邮件 */
			$cfg = $_CFG['send_ship_email'];
			if ($cfg == '1')
			{
				$tpl = get_mail_template('deliver_notice');
				$smarty->assign('order', $order);
				$smarty->assign('send_time', local_date($_CFG['time_format']));
				$smarty->assign('shop_name', $_CFG['shop_name']);
				$smarty->assign('send_date', local_date($_CFG['date_format']));
				$smarty->assign('sent_date', local_date($_CFG['date_format']));
				$smarty->assign('confirm_url', $os->url() . 'receive.php?id=' . $order['order_id'] . '&con=' . rawurlencode($order['consignee']));
				$smarty->assign('send_msg_url',$os->url() . 'user.php?message_list&order_id=' . $order['order_id']);
				$content = $smarty->fetch('str:' . $tpl['template_content']);
				if (!send_mail($order['consignee'], $order['email'], $tpl['template_subject'], $content, $tpl['is_html']))
				{
					$msg = '发送邮件失败';
				}
			}

			/* 如果需要，发短信 */
			if ($GLOBALS['_CFG']['sms_order_shipped'] == '1' && $order['mobile'] != '')
			{
				include_once('../includes/cls_sms.php');
				$sms = new sms();
				$sms->send($order['mobile'], sprintf('您的订单%s已于%s发货 [%s]', $order['order_sn'],
				local_date('m月j日G时'), $GLOBALS['_CFG']['shop_name']), 0);
			}

			/* 清除缓存 */
			clear_cache_files();
		}
	}

	$smarty->assign('msg_flag', $msg_flag);
	$smarty->assign('goods_list', $goods_list);
	
	/* 取得区域名 */
	$sql = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
	"'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
	"FROM " . $os->table('order_info') . " AS o " .
	"LEFT JOIN " . $os->table('region') . " AS c ON o.country = c.region_id " .
	"LEFT JOIN " . $os->table('region') . " AS p ON o.province = p.region_id " .
	"LEFT JOIN " . $os->table('region') . " AS t ON o.city = t.region_id " .
	"LEFT JOIN " . $os->table('region') . " AS d ON o.district = d.region_id " .
	"WHERE o.order_id = '$order[order_id]'";
	$order['region'] = $db->getOne($sql);

	/* 模板赋值 */
	$smarty->assign('ur_here', '扫描出库');

	$smarty->assign('action_link', array('href' => 'order.php?act=list', 'text' => '订单列表'));

	$smarty->assign('full_page',        1);

	/* 显示模板 */
	assign_query_info();

	$where = "where op.log_info like '%".$order_sn."'";
	$where_print = $where." and op.print_type = 1";
	$where_delivery_print = $where." and op.print_type = 3";

	$sql = 'SELECT COUNT(*) FROM ' .$GLOBALS['os']->table('order_printlog'). ' AS op ' . $where_print;		
	$count = $GLOBALS['db']->getOne($sql);    				
	$order_print_flag = $count >0 ?1:0;
	
	$sql = 'SELECT COUNT(*) FROM ' .$GLOBALS['os']->table('order_printlog'). ' AS op ' . $where_delivery_print;		
	$count = $GLOBALS['db']->getOne($sql);    				
	$order_delivery_print_flag = $count >0 ?1:0;					

//	$guide=lattice_setting('order_view');
//	//$guide['level_two']="order_ship";
//	//$guide['level_three']="";
//	$smarty->assign('guide',$guide);

	$sql = 'select pay_id from '.$GLOBALS['os']->table('payment')." WHERE is_cod = 1 ";
	$is_code = $GLOBALS['db']->getOne($sql);

	if($order['pay_id'] == $is_code){
		$smarty->assign('is_code',1);
	}
	$order['order_time']    = local_date($_CFG['time_format'], $order['add_time']);
   	$order['pay_time']      = $order['pay_time'] > 0 ?
   	local_date($_CFG['time_format'], $order['pay_time']) : $_LANG['ps'][PS_UNPAYED];
   	//$order['shipping_time'] = $order['shipping_time'] > 0 ?
   	//local_date($_CFG['time_format'], $order['shipping_time']) : $_LANG['ss'][SS_UNSHIPPED];
   	$order['address'] = str_replace("\""," ",$order['address']);
	$smarty->assign('order',$order);
	
	//*****************************************************************
	
	if($order['shipping_id']>0){
		$smarty->assign('print_time',   date('y:m:d',time()));
		//发货地址所在地
		$region_array = array();
		$region_id = !empty($_CFG['shop_country']) ? $_CFG['shop_country'] . ',' : '';
		$region_id .= !empty($_CFG['shop_province']) ? $_CFG['shop_province'] . ',' : '';
		$region_id .= !empty($_CFG['shop_city']) ? $_CFG['shop_city'] . ',' : '';
		$region_id = substr($region_id, 0, -1);
		$region = $db->getAll("SELECT region_id, region_name FROM " . $os->table("region") . " WHERE region_id IN ($region_id)");
		if (!empty($region))
		{
			foreach($region as $region_data)
			{
				$region_array[$region_data['region_id']] = $region_data['region_name'];
			}
		}
		$smarty->assign('is_cod',$order['pay_name']=='货到付款'?'√':'');
		$smarty->assign('shop_name',    str_replace("\""," ",$_CFG['shop_name']));
		$smarty->assign('province', $region_array[$_CFG['shop_province']]);
		$smarty->assign('city', $region_array[$_CFG['shop_city']]);
		$smarty->assign('shop_address', str_replace("\""," ",$_CFG['shop_address']));
		$smarty->assign('service_phone',str_replace("\""," ",$_CFG['service_phone']));
		$model_img = $db->getOne("SELECT model_img FROM " . $os->table("shipping") . " WHERE shipping_id=" . $order['shipping_id']);
		$smarty->assign('model_img',$model_img);
		$shipping_print = $db->getOne("SELECT shipping_print FROM " . $os->table("shipping") . " WHERE shipping_id=" . $order['shipping_id']);

		if (!empty($shipping_print))
		{
			order_print_log($order['order_sn'],3);
		}
		else
		{
			$shipping_code = $db->getOne("SELECT shipping_code FROM " . $os->table('shipping') . " WHERE shipping_id=" . $order['shipping_id']);
			if(!empty($shipping_code)){
				include_once(ROOT_PATH . 'includes/modules/shipping/' . $shipping_code . '.php');
			}
			if (!empty($_LANG['shipping_print']))
			{
				$shipping_print = $_LANG['shipping_print'];
			}
		}
		$smarty->assign('shipping_print',$smarty->fetch("str:$shipping_print"));
	}
	//*******************************************************************
	
	/* 是否打印订单，分别赋值 */
	if (isset($_GET['print']))
	{
		$smarty->assign('shop_name',    $_CFG['shop_name']);
		$smarty->assign('shop_url',     $os->url());
		$smarty->assign('shop_address', $_CFG['shop_address']);
		$smarty->assign('service_phone',$_CFG['service_phone']);
		$smarty->assign('print_time',   local_date($_CFG['time_format']));
		$smarty->assign('action_user',  $_SESSION['admin_name']);

		order_print_log($order['order_sn'],1);

		$smarty->template_dir = '../' . DATA_DIR;
		//$smarty->display('order_print.html');
		$smarty->display('order_ship_print.html');
	}
	
	$smarty->assign('print_flag',$order_print_flag);
	$smarty->assign('delivery_print_flag',$order_delivery_print_flag);    		
	$smarty->assign('order_sn',$order_sn);
	$smarty->assign('ship_sn',$ship_sn);


	$smarty->display('order_ship.tpl');
}

elseif($_REQUEST['act']=='weigh_goods_list'){
	
	/* 检查权限 */
	admin_priv('weigh_goods_list');
    
	/* 模板赋值 */
	$smarty->assign('ur_here', '发货称重订单列表');
	$smarty->assign('action_link', array('href' => 'order.php?act=order_query', 'text' => '订单查询'));
	
	$smarty->assign('full_page',        1);
	
    require_once(SOURCE_PATH . 'languages/zh_cn/admin/order.php');		
	$smarty->assign('lang', $_LANG);
    $smarty->assign('status_list', $_LANG['cs']);   // 订单状态

    $type = 'weigh';
	$order_list = order_list($type);
	$smarty->assign('order_list',   $order_list['orders']);
	$smarty->assign('filter',       $order_list['filter']);
	$smarty->assign('record_count', $order_list['record_count']);
	$smarty->assign('page_count',   $order_list['page_count']);
	print_r($order_list);
	/* 显示模板 */
	assign_query_info();
	$smarty->display('order_weigh_goods_list.tpl');		
}

elseif($_REQUEST['act']=='weigh_goods_query'){	

	$type = 'weigh';
    $list = order_list($type);
    $smarty->assign('order_list',           $list['orders']);
    $smarty->assign('filter',         		$list['filter']);
    $smarty->assign('record_count',   		$list['record_count']);
    $smarty->assign('page_count',     		$list['page_count']);

    require_once(SOURCE_PATH . 'languages/zh_cn/admin/order.php');		
	$smarty->assign('lang', $_LANG);
	 
    make_json_result($smarty->fetch('order_weigh_goods_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));  
}

elseif($_REQUEST['act']=='weigh_goods'){
	if(empty($_GET['order_id'])){
		sys_msg('invalid param');
		exit();		
	}
	$order_id = intval($_GET['order_id']);
	$order = order_info($order_id);
	
	if(empty($order) || $order['is_weigh']==1){
		sys_msg('订单不存在或者已经称重');
		exit();				
	}	
	/* 模板赋值 */
	$smarty->assign('ur_here', '确认发货称重订单');

	$smarty->assign('action_link', array('href' => 'order2.php?act=weigh_goods_list', 'text' => '发货称重订单列表'));
	
	$smarty->assign('order', $order);
	
	/* 显示模板 */
	assign_query_info();
	$smarty->display('order_weigh_goods_info.tpl');
}

elseif($_REQUEST['act']=='do_weigh_goods'){
	if(empty($_POST['order_id'])){
		sys_msg('invalid param');
		exit();		
	}
	$order_id = intval($_POST['order_id']);
	$order = order_info($order_id);
	if(empty($order) || $order['is_weigh']==1){
		sys_msg('订单不存在或者已经称重');
		exit();				
	}
	$weigh       = strval(trim($_POST['weigh']));
	$weigh_brief = strval(trim($_POST['weigh_brief']));
	update_order($order_id, array('is_weigh'=>1,'weigh'=>$weigh,'weigh_brief'=>$weigh_brief));

    /* 记日志 */
    admin_log(sprintf('订单【%s】称重', $order['order_sn']), 'update', 'weigh');

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    $links = array(
        array('href' => 'order2.php?act=weigh_goods_list', 'text' => '返回发货称重订单列表')
    );
    sys_msg('确认发货称重订单成功', 0, $links); 	
}

elseif($_REQUEST['act']=='list_storekeeper_type'){
	/* 检查权限 */
	admin_priv('storekeeper_type_list');

	/* 模板赋值 */
	$smarty->assign('ur_here', '仓库管理员类型列表');
	$smarty->assign('action_link', array('href' => 'order2.php?act=add_storekeeper_type', 'text' => '添加仓库管理员类型'));
	
	$smarty->assign('full_page',        1);

	$type_list = get_storekeeper_typelist();
	$smarty->assign('type_list',    $type_list['types']);
	$smarty->assign('filter',       $type_list['filter']);
	$smarty->assign('record_count', $type_list['record_count']);
	$smarty->assign('page_count',   $type_list['page_count']);
	
	/* 显示模板 */
	assign_query_info();
	$smarty->display('storekeeper_type_list.tpl');		
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query_storekeeper_type')
{
    $list = get_storekeeper_typelist();
    $smarty->assign('type_list',      $list['types']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);
    
    make_json_result($smarty->fetch('storekeeper_type_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif ($_REQUEST['act'] == 'add_storekeeper_type' || $_REQUEST['act'] == 'edit_storekeeper_type')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_storekeeper_type';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_storekeeper_type' : 'edit_storekeeper_type';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_storekeeper_type' : 'update_storekeeper_type');

    /* 初始化、取得仓库信息 */
    if ($is_add)
    {
        $type = array(
            'type_id'     		    => 0,
            'type_name'   		    => '',
            'type_desc'   		    => ''
        );
        
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }

	    $type_id = intval($_GET['id']);
	    $type = $db->getRow("SELECT * FROM ".$GLOBALS['os']->table('storekeeper_type')." WHERE type_id='$type_id'");
	    if (empty($type))
	    {
	        sys_msg('仓库管理员类型不存在');
	    }
    }
  
    $smarty->assign('type', $type);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加仓库管理员类型');
    }
    else
    {
        $smarty->assign('ur_here', '编辑仓库管理员类型');
    }
    
    if ($is_add)
    {
        $href = 'order2.php?act=list_storekeeper_type';
    }
    else
    {
        $href = 'order2.php?act=list_storekeeper_type&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '仓库管理员类型列表'));
    
    assign_query_info();
    
    $smarty->display('storekeeper_type_info.tpl');     
}

elseif ($_REQUEST['act'] == 'insert_storekeeper_type' || $_REQUEST['act'] == 'update_storekeeper_type')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_storekeeper_type';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_storekeeper_type' : 'edit_storekeeper_type';
    admin_priv($priv_str);

    /* 提交值 */
    $type = array(
        'type_id'     			    => intval($_POST['id']),
        'type_name'   			    => sub_str($_POST['type_name'], 100, false),    
		'type_desc'      			=> !empty($_POST['type_desc']) ? strval($_POST['type_desc']) : ''
    );
    
    /* 判断名称是否重复 */
    if (!$exc_type->is_only('type_name', $type['type_name'], $type['type_id']))
    {
        sys_msg('该类型名称已存在，请您换一个名称');
    }
  
    /* 保存供应商类型信息 */
    if ($is_add)
    {
        $db->autoExecute($os->table('storekeeper_type'), $type, 'INSERT');
        $type['type_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('storekeeper_type'), $type, 'UPDATE', "type_id = '$type[type_id]'");
    }
   
    /* 记日志 */
    if ($is_add)
    {
        admin_log($type['type_name'], 'add', 'storekeeper_type');
    }
    else
    {
        admin_log($type['type_name'], 'edit', 'storekeeper_type');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'order2.php?act=add_storekeeper_type', 'text' => '继续添加新的仓库管理员类型'),
            array('href' => 'order2.php?act=list_storekeeper_type', 'text' => '返回仓库管理员类型列表')
        );
        sys_msg('添加仓库管理员类型成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'order2.php?act=list_storekeeper_type&' . list_link_postfix(), 'text' => '返回仓库管理员类型列表')
        );
        sys_msg('编辑仓库管理员类型成功', 0, $links);
    }	
}

elseif($_REQUEST['act']=='remove_storekeeper_type'){
    check_authz_json('drop_storekeeper_type');

    $type_id = intval($_GET['id']);
    $type = $db->getRow("SELECT * FROM ".$GLOBALS['os']->table('storekeeper_type')." WHERE type_id='$type_id'");
    if (empty($type))
    {
        sys_msg('仓库管理员类型不存在');
    }

    if($db->query("DELETE FROM " . $os->table('storekeeper_type') . " WHERE type_id='$type_id'")){
	    /* 记日志 */
	    admin_log(sprintf('仓库管理员类型 s%', $type['type_name']), 'remove', 'storekeeper_type');	
	    $db->query("UPDATE " . $os->table('storekeeper') . " SET type_id=0 WHERE type_id='$type_id'");
    }
    
    /* 清除缓存 */
    clear_cache_files();    
    
    $url = 'order2.php?act=query_storekeeper_type&' . str_replace('act=remove_storekeeper_type', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;		
}

elseif($_REQUEST['act']=='list_storekeeper'){
	/* 检查权限 */
	admin_priv('storekeeper_list');

	/* 模板赋值 */
	$smarty->assign('ur_here', '仓库管理员列表');
	$smarty->assign('action_link', array('href' => 'order2.php?act=add_storekeeper', 'text' => '添加仓库管理员'));
	
	$smarty->assign('full_page',        1);

	$smarty->assign('storekeeper_type_list', get_storekeeper_types());
	
	$list = get_storekeeper_list();
	$smarty->assign('storekeeper_list',    $list['items']);
	$smarty->assign('filter',       $list['filter']);
	$smarty->assign('record_count', $list['record_count']);
	$smarty->assign('page_count',   $list['page_count']);
	
	/* 显示模板 */
	assign_query_info();
	$smarty->display('storekeeper_list.tpl');		
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query_storekeeper')
{
    $list = get_storekeeper_list();
    $smarty->assign('storekeeper_list',  $list['items']);
    $smarty->assign('filter',            $list['filter']);
    $smarty->assign('record_count',      $list['record_count']);
    $smarty->assign('page_count',        $list['page_count']);
    
    make_json_result($smarty->fetch('storekeeper_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif ($_REQUEST['act'] == 'add_storekeeper' || $_REQUEST['act'] == 'edit_storekeeper')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_storekeeper';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_storekeeper' : 'edit_storekeeper';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_storekeeper' : 'update_storekeeper');

    /* 初始化、取得仓库信息 */
    if ($is_add)
    {
        $sk = array(
            'sk_id'     		    => 0,
            'type_id'   		    => 0,
            'sk_name'               => '',
            'sk_code'   		    => ''
        );
        
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }

	    $sk_id = intval($_GET['id']);
	    $sk = $db->getRow("SELECT * FROM ".$GLOBALS['os']->table('storekeeper')." WHERE sk_id='$sk_id'");
	    if (empty($sk))
	    {
	        sys_msg('仓库管理员不存在');
	    }
    }
    $sk['type_list']		= 	get_storekeeper_types();
    $smarty->assign('sk', $sk);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加仓库管理员');
    }
    else
    {
        $smarty->assign('ur_here', '编辑仓库管理员');
    }
    
    if ($is_add)
    {
        $href = 'order2.php?act=list_storekeeper';
    }
    else
    {
        $href = 'order2.php?act=list_storekeeper&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '仓库管理员列表'));
    
    assign_query_info();    
    $smarty->display('storekeeper_info.tpl');     
}

elseif ($_REQUEST['act'] == 'insert_storekeeper' || $_REQUEST['act'] == 'update_storekeeper')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_storekeeper';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_storekeeper' : 'edit_storekeeper';
    admin_priv($priv_str);

    /* 提交值 */
    $sk = array(
        'sk_id'     			    => intval($_POST['id']),
        'type_id'                   => intval($_POST['type_id']),
        'sk_name'   			    => sub_str($_POST['sk_name'], 100, false),    
		'sk_code'   			    => sub_str($_POST['sk_code'], 80, false)
    );
    
    /* 判断名称是否重复 */
    if (!$exc_sk->is_only('sk_name', $sk['sk_name'], $sk['sk_id']))
    {
        sys_msg('该仓管名称已存在，请您换一个名称');
    }

    if (!$exc_sk->is_only('sk_code', $sk['sk_code'], $sk['sk_id']))
    {
        sys_msg('该仓管代码已存在，请您换一个代码');
    }
        
    /* 保存供应商类型信息 */
    if ($is_add)
    {
    	$sk['add_time'] = gmtime();
        $db->autoExecute($os->table('storekeeper'), $sk, 'INSERT');
        $sk['sk_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('storekeeper'), $sk, 'UPDATE', "sk_id = '$sk[sk_id]'");
    }
   
    /* 记日志 */
    if ($is_add)
    {
        admin_log($sk['sk_name'], 'add', 'storekeeper');
    }
    else
    {
        admin_log($sk['sk_name'], 'edit', 'storekeeper');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'order2.php?act=add_storekeeper', 'text' => '继续添加新的仓库管理员'),
            array('href' => 'order2.php?act=list_storekeeper', 'text' => '返回仓库管理员列表')
        );
        sys_msg('添加仓库管理员成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'order2.php?act=list_storekeeper&' . list_link_postfix(), 'text' => '返回仓库管理员列表')
        );
        sys_msg('编辑仓库管理员成功', 0, $links);
    }	
}

elseif($_REQUEST['act']=='remove_storekeeper'){
    check_authz_json('drop_storekeeper');

    $sk_id = intval($_GET['id']);
    $sk    = $db->getRow("SELECT * FROM ".$GLOBALS['os']->table('storekeeper')." WHERE sk_id='$sk_id'");
    if(empty($sk))
    {
        sys_msg('仓库管理员不存在');
    }

    if($db->query("DELETE FROM " . $os->table('storekeeper') . " WHERE sk_id='$sk_id'")){
	    /* 记日志 */
	    admin_log(sprintf('仓库管理员 s%', $sk['sk_name']), 'remove', 'storekeeper');	
    }
    
    /* 清除缓存 */
    clear_cache_files();    
    
    $url = 'order2.php?act=query_storekeeper&' . str_replace('act=remove_storekeeper', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;		
}

function get_storekeeper_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'sk_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        $filter['type_id']    = empty($_REQUEST['type_id']) ? 0 : intval($_REQUEST['type_id']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }     
           
        $where = 'WHERE 1 ';
        
        if ($filter['type_id'])
        {
			$where .= " AND s.type_id=$filter[type_id]";
        } 
                
        if ($filter['keyword'])
        {
			$where .= " AND (s.sk_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR s.sk_code LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }  
         
        /* 查询记录总数，计算分页数 */
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('storekeeper') . " s LEFT JOIN " . 
        	   $GLOBALS['os']->table('storekeeper_type') . " t ON s.type_id=t.type_id $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT s.*, t.type_name FROM " . $GLOBALS['os']->table('storekeeper') . " s LEFT JOIN " . $GLOBALS['os']->table('storekeeper_type') . 
               " t ON s.type_id=t.type_id $where ORDER BY $filter[sort_by] $filter[sort_order]";
               
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }

    return array('items' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}

function get_storekeeper_types(){
	$arr=array();
	$sql = "SELECT type_id,type_name FROM " . $GLOBALS['os']->table('storekeeper_type');
	$result = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		$arr[$row['type_id']] = $row['type_name'];
	}
	return $arr;
}

function get_storekeeper_typelist(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'type_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }     
           
        $where = 'WHERE 1 ';
        if ($filter['keyword'])
        {
			$where .= " AND (type_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR type_desc LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }   
        /* 查询记录总数，计算分页数 */
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('storekeeper_type') . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM " . $GLOBALS['os']->table('storekeeper_type') . " $where ORDER BY $filter[sort_by] $filter[sort_order]";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }

    return array('types' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function get_shipped_order_list(){

	$sql = 'select * from '.$GLOBALS['os']->table('order_info')." WHERE shipping_status = 1 ";
	if($_REQUEST['shipping_id'] != '0'){
		$sql.=' and shipping_id = '.$_REQUEST['shipping_id'];
	}
	if($_REQUEST['search_time']=='1'){
		
		if(!empty($_REQUEST['start_time_id'])){
			$sql.=' and shipping_time >= '.local_strtotime($_REQUEST['start_time_id']);
		}
		if(!empty($_REQUEST['end_time_id'])){
			$sql.=' and shipping_time <= '.local_strtotime($_REQUEST['end_time_id']);
		}
		
	}elseif($_REQUEST['search_time']=='0'){

		$start = mktime(0,0,0,date('m'),date('d'),date('y'));
		$end = mktime(23,59,59,date('m'),date('d'),date('y'));
		
		$sql.=' and shipping_time >= '.$start;
		$sql.=' and shipping_time <= '.$end;
	}
	
	$sql.=' order by shipping_time asc';
	//die($sql);
	$row = $GLOBALS['db']->getAll($sql);
	/* 格式化数据 */
	foreach ($row AS $key => $value)
	{
		$row[$key]['formated_shipping_time'] = local_date('Y-m-d H:i', $value['shipping_time']);
		$row[$key]['formated_add_time'] = local_date('Y-m-d H:i', $value['add_time']);
	}
	$arr = array('orders' => $row, 'record_count' => sizeof($row));
	return $arr;
}

function query_order_by_goods_sn(){
	
	$goods_sn = $_REQUEST['goods_sn'];
	if(empty($goods_sn)) return ;
	
	$goods_color = $_REQUEST['goods_color'];
	$goods_size = $_REQUEST['goods_size'];
	
	$sql = "select o.order_sn, o.shipping_name ,o.invoice_no from open_order_goods as g 
			left join open_order_info as o on g.order_id = o.order_id
			left join open_color as c on g.color_id = c.color_id
			left join open_size as s on g.size_id = s.size_id where shipping_status = 1 and  goods_sn = '".$goods_sn."' ";
	
	if(!empty($goods_color)){
		$sql.=" and c.color_code = '".$goods_color."' ";
	}
	if(!empty($goods_size)){
		$sql.=" and s.size_note = '".$goods_size."' ";
	}
	
	// 获取时间参数
	if($_REQUEST['search_time2']=='1'){
		
		if(!empty($_REQUEST['start_time_id2'])){
			$sql.=' and o.shipping_time >= '.local_strtotime($_REQUEST['start_time_id2']);
		}
		if(!empty($_REQUEST['end_time_id2'])){
			$sql.=' and o.shipping_time <= '.local_strtotime($_REQUEST['end_time_id2']);
		}
		
	}elseif($_REQUEST['search_time2']=='0'){
		
		$start = mktime(0,0,0,date('m'),date('d'),date('y'));
		$end = mktime(23,59,59,date('m'),date('d'),date('y'));
		
		$sql.=' and o.shipping_time >= '.$start;
		$sql.=' and o.shipping_time <= '.$end;
		
	}
	$row = $GLOBALS['db']->getAll($sql);
	
	return $row;
	
}

function query_order_shipped(){

//	$sql = "select o.order_sn, o.shipping_name ,o.invoice_no from open_order_goods as g 
//			left join open_order_info as o on g.order_id = o.order_id
//			left join open_color as c on g.color_id = c.color_id
//			left join open_size as s on g.size_id = s.size_id where shipping_status = 1 ";
	
	$sql = "SELECT o.order_id,o.order_sn,o.shipping_status,o.to_buyer,o.invoice_no,shipping_name, from_unixtime(o.shipping_time) as shipping_time, sum(g.goods_number) number 
			from open_order_goods as g 
			left join open_order_info as o on  g.order_id = o.order_id 
			left join open_size as s on g.size_id = s.size_id where 1 ";
	
	
	// 获取时间参数
	if($_REQUEST['search_time3']=='1'){
		
		if(!empty($_REQUEST['start_time_id3'])){
			$sql.=' and o.shipping_time >= '.local_strtotime($_REQUEST['start_time_id3']);
		}
		if(!empty($_REQUEST['end_time_id3'])){
			$sql.=' and o.shipping_time <= '.local_strtotime($_REQUEST['end_time_id3']);
		}
		
	}elseif($_REQUEST['search_time3']=='0'){
		
		$start = mktime(0,0,0,date('m'),date('d'),date('y'));
		$end = mktime(23,59,59,date('m'),date('d'),date('y'));
		
		$sql.=' and o.shipping_time >= '.$start;
		$sql.=' and o.shipping_time <= '.$end;
		
	}
	
	if($_REQUEST['shipping_sel']!='-1'){
		$sql.=' and o.shipping_status = '.$_REQUEST['shipping_sel'];
	}
	$sql.=' group by o.order_sn ';
	
	$row = $GLOBALS['db']->getAll($sql);
	
	return $row;
	
}

?>